//
//  TSHomeNewController.m
//  chart
//
//  Created by 曾斌 on 16/5/1.
//  Copyright (c) 2016年 xu. All rights reserved.
//

#import "TSHomeNewController.h"
#import "TSMsgTabBarController.h"
#import "TSReceiveTabBarController.h"
#import "TSWebViewController.h"
#define TOP_IMAGE_HEIGHT 120.f
#define COLUM  3
#define SPACING 1
#define GRIDHEIGHT 80
@implementation TSHomeNewController
{
    UIScrollView *sview;
    
    UILabel * infoLabel;
    UILabel * taskLabel;
    UILabel * otherLabel;
    UIView * infoGrid;
    UIView * taskGrid;
    UIView * otherGrid;

    int gridWidth;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"主界面";
    self.navigationController.navigationBarHidden = YES;
    UIImage *draw = [UIImage imageNamed:@"main_bg"];
    UIImageView *drawView = [[UIImageView alloc]initWithImage:draw];
    drawView.frame = CGRectMake(0, 0, currentDeviceWidth, TOP_IMAGE_HEIGHT);
    [self.view addSubview:drawView];
//    sview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TOP_IMAGE_HEIGHT, currentDeviceWidth, currentDeviceHeight-240)];
//    [sview setContentSize:CGSizeMake(currentDeviceWidth,currentDeviceHeight-240)];
//    sview.scrollEnabled = YES;
//    [sview setUserInteractionEnabled:YES];
    [self initUI];
    
    [self.view addSubview:sview];

}

-(void)initUI{
    [self addInfoLabel];
    [self addInfoGrid];
    [self addTaskLabel];
    [self addTaskGrid];
    [self addOtherLabel];
    [self addOtherGrid];
}

-(void)infoGridClick:(UITapGestureRecognizer *)tap{
    UIView * sender = tap.view;
    if(sender.tag == 0){
         //个人任务
    }else if(sender.tag == 1){
     //个人学习
        TSWebViewController *web_vc=[[TSWebViewController alloc] initWithTitle:@"个人学习" andUrl:kRequestUrl_Study_outWeb_content];
        TSBaseNavigationController *nav_vc_msg = [[TSBaseNavigationController alloc] initWithRootViewController:web_vc];
        [self presentViewController:nav_vc_msg animated:YES completion:nil];
    }else if(sender.tag == 2){
        //工作落实
    }else if(sender.tag == 3){
        //项目跟踪
    }else if(sender.tag == 4){
         //办公助手
    }else if(sender.tag == 5){
        //事件通知
        TSMsgTabBarController *tab_bar = [[TSMsgTabBarController alloc] init];
        [self presentViewController:tab_bar animated:YES completion:nil];
    }
}
-(void)taskGridClick:(UITapGestureRecognizer *)tap{
        UIView * sender = tap.view;
    if(sender.tag == 0){
        TSReceiveTabBarController * tab_bar =  [[TSReceiveTabBarController alloc] init];
        [self presentViewController:tab_bar animated:YES completion:nil];
    }else if(sender.tag == 1){
    
    }
}


-(void)otherGridClick:(UITapGestureRecognizer *)tap{
}
-(void)addInfoGrid{
    infoGrid = [[UIView alloc] init];
    gridWidth = (currentDeviceWidth -2*SPACING)/COLUM;
    for(int i=0;i<6;i++){
      
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_info_%zd", i+1]]];
        int y = i/COLUM*(SPACING+GRIDHEIGHT);
        int x = i%COLUM*(SPACING+gridWidth);
        img.frame =CGRectMake(x, y, gridWidth, GRIDHEIGHT);
        [img setTag:i];
        [img setUserInteractionEnabled:YES];
        [img addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(infoGridClick:)]];
        [infoGrid addSubview:img];
    }
    [self.view addSubview:infoGrid];
}

-(void)addTaskGrid{
    taskGrid = [[UIView alloc] init];
    for(int i=0;i<3;i++){
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_task_%zd.jpg", i+1]]];
        int y = i/COLUM*(SPACING+GRIDHEIGHT);
        int x = i%COLUM*(SPACING+gridWidth);
        img.frame =CGRectMake(x, y, gridWidth, GRIDHEIGHT);
        
        [img setTag:i];
        [img setUserInteractionEnabled:YES];
        UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taskGridClick:)];
        [img addGestureRecognizer:singleTap1];
        [taskGrid addSubview:img];
    }
    [self.view addSubview:taskGrid];
    
}

-(void)addOtherGrid{
    otherGrid = [[UIView alloc] init];
    for(int i=0;i<2;i++){
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_other_%zd", i+1]]];
        int y = i/COLUM*(SPACING+GRIDHEIGHT);
        int x = i%COLUM*(SPACING+gridWidth);
        img.frame =CGRectMake(x, y, gridWidth, 50);
        [img setTag:i];
        [img setUserInteractionEnabled:YES];
        UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(otherGridClick:)];
        [img addGestureRecognizer:singleTap1];
        [otherGrid addSubview:img];

    }
     [self.view addSubview:otherGrid];
}

-(UILabel *)createLabel{
    UILabel * label = [[UILabel alloc] init];
    label.font = FONT(10);
    label.backgroundColor = RGB(200, 200, 200);
    return label;
}
-(void)addInfoLabel{
    infoLabel = [self createLabel];
    [infoLabel setText:@"信息管理"];
    [self.view addSubview:infoLabel];
}
-(void)addTaskLabel{
    taskLabel = [self createLabel];
    [taskLabel setText:@"任务统计"];
    [self.view addSubview:taskLabel];
}
-(void)addOtherLabel{
    otherLabel = [self createLabel];
    [otherLabel setText:@"其他管理"];
    [self.view addSubview:otherLabel];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    infoLabel.frame =CGRectMake(0, TOP_IMAGE_HEIGHT, currentDeviceWidth, 20);
    infoGrid.frame = CGRectMake(0, infoLabel.frame.origin.y+infoLabel.frame.size.height, currentDeviceWidth, GRIDHEIGHT*2+SPACING);
    taskLabel.frame =CGRectMake(0, infoGrid.frame.origin.y+infoGrid.frame.size.height, currentDeviceWidth, 20);
    taskGrid.frame = CGRectMake(0, taskLabel.frame.origin.y+taskLabel.frame.size.height, currentDeviceWidth, GRIDHEIGHT);
    otherLabel.frame =CGRectMake(0, taskGrid.frame.origin.y+taskGrid.frame.size.height, currentDeviceWidth, 20);
    otherGrid.frame =CGRectMake(0, otherLabel.frame.origin.y+otherLabel.frame.size.height, currentDeviceWidth, GRIDHEIGHT);
}
@end
